package cn.kgc.springboot.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author: mosin
 * @version: v1.0  2022/11/10
 */
@Component
public class RedisUtil {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    // 设置string类型的数据
    public void setValue(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }

    // 设置值之前 先判定key是否存在
    public  void  setIfAbsent(String key,String value){
        redisTemplate.opsForValue().setIfAbsent(key, value);
    }

    //  修改
    public void  setIfPresent(String key,String value){
        redisTemplate.opsForValue().setIfPresent(key, value);
    }

    // 添加string类型的值 同时设置存活时间
    public void setValue(String key, String value, Long timeout, TimeUnit timeUnit) {
        redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
    }

    //获取指定key的值
    public String  getValue(String key){
        return  redisTemplate.opsForValue().get(key);
    }

    //删除指定key
    public Boolean  deleteByKey(String key){
       return redisTemplate.delete(key);
    }

    // 是否包含指定key
    public Boolean  hasKey(String key){
        return redisTemplate.hasKey(key);
    }


}
